package com.yantong.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.yantong.pojo.bo.PrdStaticDataReqDto;
import com.yantong.pojo.vo.ProdTotalAndSalVo;
import com.yantong.pojo.vo.ProdVoStatic;
import com.yantong.utils.PageUtils;
import com.yantong.mapper.ProductCentreMapper;
import com.yantong.pojo.vo.ProdAfterSaleVo;
import com.yantong.mapper.ReportMapper;
import com.yantong.service.ProductStaticService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductStaticServiceImpl implements ProductStaticService{
	@Autowired
	private ReportMapper reportMapper;
	@Autowired
	private ProductCentreMapper productCentre;

	@Override
	public PageUtils<ProdAfterSaleVo> prodstaticData(PrdStaticDataReqDto reqDto) {
		PageUtils<ProdAfterSaleVo> pageUtils = new PageUtils<>();
		int total=reportMapper.countProdstaticData(reqDto);
		Map<String,Object>queryMap = new HashMap<>();
		queryMap.put("startTime", reqDto.getStartTime());
		queryMap.put("endTime", reqDto.getEndTime());
		//List<String> centreIds = productCentre.selectCentreIdsInProductId();
		List<ProdAfterSaleVo> list = reportMapper.prodstaticData(reqDto, new RowBounds(reqDto.getOffset(), reqDto.getLimit()));
		
		//统计平台销售总额
		// queryMap.put("centreIds",centreIds);
		String salesAllAmt1=reportMapper.countSalesAllAmt(queryMap);
		//统计平台售后总额
		String salesAfterAllAmt1= reportMapper.countSalesAfterAllAmt(queryMap);
		for(int i=0;i<list.size();i++){
			//查询到base id 下centre 表中关联的所有 商品 id （centreid）
			List<String>centrIds=productCentre.selectCentreIdsByProductId(list.get(i).getId());
			if(CollectionUtils.isEmpty(centrIds)){
				list.get(i).setSalesAllAmt(salesAllAmt1);
				list.get(i).setSalesAfterAllAmt(salesAfterAllAmt1);
				list.get(i).setSalVolus("0");
				list.get(i).setSalesAmount("0.00");
				list.get(i).setAfterSaleCount("0");
				list.get(i).setAfterSaleAmt("0.00");
				list.get(i).setProdSaleAfterRate("0.00");
				list.get(i).setDiffr("0.00");
				list.get(i).setProdSaleAfterRate("0.00");
				list.get(i).setSalesAfterRate("0.00");
			}else{
				
				//统计单个商品该时间段销量
				queryMap.put("ids", centrIds);
				list.get(i).setSalesAllAmt(StringUtil.isEmpty(salesAllAmt1)?"0":salesAllAmt1);
				list.get(i).setSalesAfterAllAmt( StringUtil.isEmpty(salesAfterAllAmt1)?"0":salesAfterAllAmt1);
				ProdTotalAndSalVo prodTotalAndSalVo = reportMapper.countSalVolusByProductId(queryMap);
				list.get(i).setSalVolus(prodTotalAndSalVo.getSalVolus());
				list.get(i).setSalesAmount(prodTotalAndSalVo.getSalesAmount());
				ProdVoStatic prodVoStatic = reportMapper.coutProdAfterStatic(queryMap);
				list.get(i).setAfterSaleCount(StringUtil.isEmpty(prodVoStatic.getAfterSaleCount())?"0":prodVoStatic.getAfterSaleCount());
				list.get(i).setAfterSaleAmt(StringUtil.isEmpty(prodVoStatic.getAfterSaleAmt())?"0.00":prodVoStatic.getAfterSaleAmt());
				BigDecimal salesAfterAllAmt=new BigDecimal(StringUtil.isEmpty(list.get(i).getSalesAfterAllAmt())?"0.00":list.get(i).getSalesAfterAllAmt());//平台售后总额
				BigDecimal salesAllAmt=new BigDecimal(StringUtil.isEmpty(list.get(i).getSalesAllAmt())?"0.00":list.get(i).getSalesAllAmt());//平台销售总额
				BigDecimal salesAfterRate = new BigDecimal("0.00");
				if(salesAllAmt.compareTo(new BigDecimal("0.00"))!=0&&salesAfterAllAmt.compareTo(new BigDecimal("0.00"))!=0){
					salesAfterRate=salesAfterAllAmt.divide(salesAllAmt, 2, RoundingMode.HALF_UP);//平台售后总额/平台销售总额
					list.get(i).setSalesAfterRate(salesAfterRate+"");
				}else{
					list.get(i).setSalesAfterRate("0.00");
				}
				
				BigDecimal afterSaleAmt=new BigDecimal(StringUtil.isEmpty(list.get(i).getAfterSaleAmt())?"0.00":list.get(i).getAfterSaleAmt());//商品收购额
				BigDecimal salesAmout=new BigDecimal(StringUtil.isEmpty(list.get(i).getSalesAmount())?"0.00":list.get(i).getSalesAmount());//商品销售额
				if(afterSaleAmt.compareTo(new BigDecimal("0.00"))!=0&&salesAmout.compareTo(new BigDecimal("0.00"))!=0){
					BigDecimal prodSaleAfterRate=afterSaleAmt.divide(salesAmout, 2, RoundingMode.HALF_UP);//商品售后总额/商品销售总额
					list.get(i).setProdSaleAfterRate(prodSaleAfterRate+"");
					list.get(i).setDiffr(salesAfterRate.subtract(prodSaleAfterRate)+"");
				}else{
					list.get(i).setProdSaleAfterRate("0.00");
					list.get(i).setDiffr(salesAfterRate+"");
				}
				
			}
			
		}
         pageUtils.setTotal(total);
         pageUtils.setRows(list);
        return pageUtils;
	}


	@Override
	public List<List<Object>> excelData(PrdStaticDataReqDto reqDto, List<List<Object>> resultData) {
		Map<String,Object>queryMap = new HashMap<>();
		queryMap.put("startTime", reqDto.getStartTime());
		queryMap.put("endTime", reqDto.getEndTime());
		//List<String> centreIds = productCentre.selectCentreIdsInProductId();
		List<ProdAfterSaleVo> list = reportMapper.prodstaticData(reqDto);
		
		//统计平台销售总额
		// queryMap.put("centreIds",centreIds);
		String salesAllAmt1=reportMapper.countSalesAllAmt(queryMap);
		//统计平台售后总额
		String salesAfterAllAmt1= reportMapper.countSalesAfterAllAmt(queryMap);
		for(int i=0;i<list.size();i++){
			List <Object> rowList = new ArrayList<>();
			//查询到base id 下centre 表中关联的所有 商品 id （centreid）
			List<String>centrIds=productCentre.selectCentreIdsByProductId(list.get(i).getId());
			if(CollectionUtils.isEmpty(centrIds)){
				list.get(i).setSalesAllAmt(salesAllAmt1);
				list.get(i).setSalesAfterAllAmt(salesAfterAllAmt1);
				list.get(i).setSalVolus("0");
				list.get(i).setSalesAmount("0.00");
				list.get(i).setAfterSaleCount("0");
				list.get(i).setAfterSaleAmt("0.00");
				list.get(i).setProdSaleAfterRate("0.00");
				list.get(i).setDiffr("0.00");
				list.get(i).setProdSaleAfterRate("0.00");
				list.get(i).setSalesAfterRate("0.00");
			}else{
				
				//统计单个商品该时间段销量
				queryMap.put("ids", centrIds);
				list.get(i).setSalesAllAmt(StringUtil.isEmpty(salesAllAmt1)?"0":salesAllAmt1);
				list.get(i).setSalesAfterAllAmt( StringUtil.isEmpty(salesAfterAllAmt1)?"0":salesAfterAllAmt1);
				ProdTotalAndSalVo prodTotalAndSalVo = reportMapper.countSalVolusByProductId(queryMap);
				list.get(i).setSalVolus(prodTotalAndSalVo.getSalVolus());
				list.get(i).setSalesAmount(prodTotalAndSalVo.getSalesAmount());
				ProdVoStatic prodVoStatic = reportMapper.coutProdAfterStatic(queryMap);
				list.get(i).setAfterSaleCount(StringUtil.isEmpty(prodVoStatic.getAfterSaleCount())?"0":prodVoStatic.getAfterSaleCount());
				list.get(i).setAfterSaleAmt(StringUtil.isEmpty(prodVoStatic.getAfterSaleAmt())?"0.00":prodVoStatic.getAfterSaleAmt());
				BigDecimal salesAfterAllAmt=new BigDecimal(StringUtil.isEmpty(list.get(i).getSalesAfterAllAmt())?"0.00":list.get(i).getSalesAfterAllAmt());//平台售后总额
				BigDecimal salesAllAmt=new BigDecimal(StringUtil.isEmpty(list.get(i).getSalesAllAmt())?"0.00":list.get(i).getSalesAllAmt());//平台销售总额
				BigDecimal salesAfterRate = new BigDecimal("0.00");
				if(salesAllAmt.compareTo(new BigDecimal("0.00"))!=0&&salesAfterAllAmt.compareTo(new BigDecimal("0.00"))!=0){
					salesAfterRate=salesAfterAllAmt.divide(salesAllAmt, 2, RoundingMode.HALF_UP);//平台售后总额/平台销售总额
					list.get(i).setSalesAfterRate(salesAfterRate+"");
				}else{
					list.get(i).setSalesAfterRate("0.00");
				}
				
				BigDecimal afterSaleAmt=new BigDecimal(StringUtil.isEmpty(list.get(i).getAfterSaleAmt())?"0.00":list.get(i).getAfterSaleAmt());//商品收购额
				BigDecimal salesAmout=new BigDecimal(StringUtil.isEmpty(list.get(i).getSalesAmount())?"0.00":list.get(i).getSalesAmount());//商品销售额
				if(afterSaleAmt.compareTo(new BigDecimal("0.00"))!=0&&salesAmout.compareTo(new BigDecimal("0.00"))!=0){
					BigDecimal prodSaleAfterRate=afterSaleAmt.divide(salesAmout, 2, RoundingMode.HALF_UP);//商品售后总额/商品销售总额
					list.get(i).setProdSaleAfterRate(prodSaleAfterRate+"");
					list.get(i).setDiffr(salesAfterRate.subtract(prodSaleAfterRate)+"");
				}else{
					list.get(i).setProdSaleAfterRate("0.00");
					list.get(i).setDiffr(salesAfterRate+"");
				}
				
			}
			rowList.add(list.get(i).getId());
			rowList.add(list.get(i).getTitle());
			rowList.add(list.get(i).getSpecTitle());
			rowList.add(list.get(i).getSalVolus());
			rowList.add(list.get(i).getCostprice());
			rowList.add(list.get(i).getMarketprice());
			rowList.add(list.get(i).getSalesAmount());
			rowList.add(list.get(i).getAfterSaleCount());
			rowList.add(list.get(i).getAfterSaleAmt());
			rowList.add(list.get(i).getSalesAllAmt());
			rowList.add(list.get(i).getSalesAfterAllAmt());
			rowList.add(list.get(i).getProdSaleAfterRate());
			rowList.add(list.get(i).getSalesAfterRate());
			rowList.add(list.get(i).getDiffr());
			rowList.add(list.get(i).getSupplier());
            resultData.add(rowList);
		}
     
        return resultData;
	}

}
